package cn.tuzirpa;

import cn.tuzirpa.config.DatabaseConfig;
import cn.tuzirpa.executor.SqlFileExecutor;
import cn.tuzirpa.manager.DatabaseManager;
import cn.tuzirpa.tracker.SqlExecutionTracker;

/**
 * 数据库初始化启动类
 * 负责初始化数据库并执行SQL文件
 */
public class DatabaseInitStart {

    private final DatabaseConfig config;
    private final String sqlDirectory;

    public DatabaseInitStart(String url, String username, String password, String databaseName, String charset, String sqlDirectory) {
        config = new DatabaseConfig(url, username, password, databaseName, charset);
        if (sqlDirectory == null || sqlDirectory.isEmpty()) {
            sqlDirectory = "sql-examples";
        }
        this.sqlDirectory = sqlDirectory;
    }

    public void start() {
        DatabaseManager databaseManager = new DatabaseManager(config);
        databaseManager.createDatabase();

        SqlExecutionTracker executionTracker = new SqlExecutionTracker(databaseManager);
        SqlFileExecutor sqlExecutor = new SqlFileExecutor(databaseManager, executionTracker);
        sqlExecutor.executeSqlFiles(sqlDirectory);

    }

}
